/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.boapi.person;

/**
 * <span lang='en'>
 * Simple implementation of national personal identity card. Today it implements:
 * <ul>
 *   <li> Documento nacional de identidad [DNI] - Identity document for spanish spoken countries</li>
 * </ul>
 * </span> 
 * <span lang ='de'>
 * Basisimplementation von Identitätsnachweisen in Abhängigkeit zum jeweiligen Land.
 * Zur Zeit werden unterstützt:
 * <ul>
 *   <li> Documento nacional de identidad [DNI] - Identitätsnachweis in Ländern mit Spanisch als Amtsprache.</li>
 * </ul>
 * </span>
 * @author Sebastian Ritter
 * @version 1.0.0.0
 */
public class SimplePersonalIdentityCardImpl implements IIdentityDocument {
	
	/**
	 * <span lang='en'>
	 * The two letter ISO 3166 country code for personal identity card.
	 * </span>
	 */
	private String twoLetterISO3166CountryCode = "  ";
	
	/**
	 * The number of personal identity card.
	 */
	private String number = "";
	
	/**
	 * <span lang='en'>
	 * Create a new personal identity object for given country.
	 * </span>
	 * @param twoLetterISO3166CountryCode
	 */
	public SimplePersonalIdentityCardImpl (final String twoLetterISO3166CountryCode) {
		if ("ES".equalsIgnoreCase(twoLetterISO3166CountryCode)) {
			this.twoLetterISO3166CountryCode = "ES";
		}
		else {
			throw new IllegalArgumentException ("Not supported country.");
		}
	}

	public boolean isChecknumberCorrect() {
		/* Sources:
		 * http://www.pruefziffernberechnung.de/D/DNI.shtml
         */
		boolean ok = false;
		//
		if ("ES".equals(this.twoLetterISO3166CountryCode)) {
			if (9 == this.number.length() && Character.isLetter(this.number.charAt(8))) {
				final int number = Integer.parseInt(this.number.substring(0,7));
				final int mod23 = number % 23;
				final char [] LETTER = "TRWAGMYFPDXBNJZSQVHLCKE".toCharArray(); //$SUP-NC$
				ok = this.number.charAt(8) == LETTER [mod23]; //$SUP-MAIOR$ - Blödsinn!
			}
		}
		return ok;
	}

	/**
	 * <span lang='en'>
	 * Set the number of personal identity card.
	 * <span lang='en'>
	 */
	public void setCompleteValue(final String[] value) {
		this.setCompleteValue(value[0]);
	}

	/**
	 * <span lang='en'>
	 * Set the number of personal identity card.
	 * <span lang='en'>
	 */
	public void setCompleteValue(final String value) {
		this.number = (null == value) ? "" : value;
	}

	/**
	 * Return the two letter ISO 3166 country code
	 * @return twoLetterISO3166CountryCode
	 */
	public String getTwoLetterISO3166CountryCode() {
		return this.twoLetterISO3166CountryCode;
	}
	
}
